<?php
class MBanner extends CI_Model{

	public function __construct()
    {
        parent::__construct();
    }
    
    function getBannerDropdown()
    {
        $data = array();
		$this->db->order_by("title");
        $Q = $this->db->get('banner');
        $data[0] = '...Select Banner...';
	    if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['banner_id']] = $row['title'];
	       }
         }
         $Q->free_result();    
	     return $data; 	
    }
    
    function getBannerById($id){
	    $data = array();
	    $this->db->where('banner_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('banner');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
     
     function getPageBannerDropdownByBannerId($id){
	    $data = array();
        $this->db->select('pagebanner.page_id, page.title');
        $this->db->from('pagebanner');
        $this->db->join('page','pagebanner.page_id = page.page_id');
        $this->db->where('banner_id',id_clean($id));
        $Q = $this->db->get();
	    if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['page_id']] = $row['page_id'];
	       }
         }
         $Q->free_result();    
	     return $data; 	 
	 }
     
     function getPageIdListByBannerId($id){
	    $data = array();
        $Q = $this->db->get('pagebanner');
        if ($Q->num_rows() > 0){
           foreach ($Q->result_array() as $row){
             $data[] = $row['page_id'];
           }
        }
	    $Q->free_result();  
	    return $data; 
	 }
	
	function getBannerByTitle($title){
	    $data = array();
	    $this->db->where('title',db_clean($title));
	    $this->db->where('activate', 1);
	    $this->db->limit(1);
	    $Q = $this->db->get('banner');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
		
	 function getBannerList(){
	     $data = array();
	     $this->db->order_by("banner_id");
	     $Q = $this->db->get('banner');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
     
     function getBannerListByPageId($pageId)
     {
        $data = array();
        $this->db->distinct();
        $this->db->select('banner.title, banner.path, banner.description');
        $this->db->from('pagebanner');
        $this->db->join('page','pagebanner.page_id = page.page_id');
        $this->db->join('banner','pagebanner.banner_id = banner.banner_id');
        $this->db->where('pagebanner.page_id', $pageId);
     	$this->db->order_by("banner.title");
        $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
     }
     
     function getMaxIdPageBanner()
     {
        $query = $this->db->query('SELECT max(id) as max_id FROM pagebanner'); 
        $row = $query->row_array();
        $max_id = $row['max_id'];  
        return $max_id != null ? $max_id : 0;
     }
     
     function addEditBanner($bannerId = 0){
		$data = array( 
			'title' => db_clean($_POST['title']),
            'description' => $_POST['description'],
			'activate' => db_clean(isset($_POST['activate']) ? 1 : 0)
		);
        $path = $this->uploadBanner();
        if($bannerId > 0)
        {
            if($path != null && $path != '')
            {
                $data['path'] = $path;
            }
            $this->db->where('banner_id', id_clean($bannerId));
    		$this->db->update('banner', $data);   
            $pages = isset($_POST['pages']) ? $_POST['pages'] : null;
            if(count($pages))
            {
                $this->deletePageBannerByBannerId($bannerId);
                $maxId = $this->getMaxIdPageBanner();
                $bannerPages = array();
                for($i = 0; $i < count($pages); $i++ )
                {
                    $newId = $maxId > 0 ? (int)($maxId+($i+1)) : (int)($maxId + $i);
                    $bannerPages[$i] = array('id'=>$newId, 'banner_id'=>$bannerId, 'page_id'=>id_clean($pages[$i]));
                }
                $this->addEditPageBanner($bannerPages);
            }
            //else, delete from pagebanner 
            else
            {
                $this->deletePageBannerByBannerId($bannerId);
            }
        }
        else
        {
            $data['path'] = $path;
            $this->db->insert('banner', $data);
            $lastId = $this->db->insert_id();
            $pages = isset($_POST['pages']) ? $_POST['pages'] : null;
            if(count($pages))
            {
                $maxId = $this->getMaxIdPageBanner();
                $bannerPages = array();
                for($i = 0; $i < count($pages); $i++ )
                {
                    $newId = $maxId > 0 ? (int)($maxId+($i+1)) : (int)($maxId + $i);
                    $bannerPages[$i] = array('id'=>$newId, 'banner_id'=>$lastId, 'page_id'=>id_clean($pages[$i]));
                }
                $this->addEditPageBanner($bannerPages);
            }
        }	 	
	 }
     
     function uploadBanner()
     {
        if ($_FILES){
		 	$path = 'media/images/banner';
		 	if(!is_dir($path))
		    {
		      mkdir($path,0755,TRUE);
		    } 
		    
			$config['upload_path'] = './'.$path;
			$config['allowed_types'] = 'gif|jpg|jpeg|png';
			$config['max_size'] = '524288';//512 mb
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
		
			if (strlen($_FILES['path']['name'])){
				if(!$this->upload->do_upload('path')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
			
				if ($image['file_name']){
					return $image['file_name'];
				}
			}
		}
        return null;
     }
	 
     function addEditPageBanner($pageArray, $bannerId = 0)
     {
        if($bannerId > 0)
        {
            for($i = 0; $i < count($pageArray); $i++ )
            {
                $this->db->where('banner_id', id_clean($bannerId));
                //$this->db->where('page_id', id_clean($pageArray['page_id']));
                $this->db->update('pagebanner', $pageArray[$i]);
            }   
        }
        else
        {
            $this->db->insert_batch('pagebanner', $pageArray);
        }	 	 
     }
     
     function deletePageBannerByBannerId($bannerId)
     {
        $this->db->where('banner_id', id_clean($bannerId));
		$this->db->delete('pagebanner');
     }
     
	 function deleteBanner($id){
	 	$this->db->where('banner_id', id_clean($id));
		$this->db->delete('banner');	
	 }
 }